package com.jindy.module.returnapplyorder.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jindy.common.core.domain.entity.SysDept;
import com.jindy.common.core.domain.entity.SysUser;
import com.jindy.common.utils.SecurityUtils;
import com.jindy.module.outboundorder.domain.OutboundOrder;
import com.jindy.module.returnapplyorder.domain.ReturnApply;
import com.jindy.module.returnapplyorder.dto.ReturnApplyDto;
import com.jindy.module.returnapplyorder.service.IReturnApplyService;
import com.jindy.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jindy.common.annotation.Log;
import com.jindy.common.core.controller.BaseController;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.enums.BusinessType;
import com.jindy.common.utils.poi.ExcelUtil;
import com.jindy.common.core.page.TableDataInfo;

/**
 * 回库申请单Controller
 *
 * @author ruoyi
 * @date 2022-10-19
 */
@RestController
@RequestMapping("/module/returnApply")
public class ReturnApplyController extends BaseController
{
    @Autowired
    private IReturnApplyService returnApplyService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询回库申请单列表
     */
    @PreAuthorize("@ss.hasPermi('module:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReturnApply returnApply)
    {
//        //添加权限
//        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
//        returnApply.setUserId(sysUser.getUserId());

//        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
//        SysDept dept =sysUser.getDept();
//        if(dept.getDeptName().indexOf("市场")>=0){
//            returnApply.setUserId(sysUser.getUserId());
//        }
        startPage();
        List<ReturnApplyDto> list = returnApplyService.selectReturnApplyList(returnApply);
        return getDataTable(list);
    }


    /**
     * 查询回库申请单列表
     */
//    @PreAuthorize("@ss.hasPermi('module:apply:childList')")
    @GetMapping("/childList")
    public TableDataInfo childList(ReturnApply returnApply)
    {
//        //添加权限
//        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
//        returnApply.setUserId(sysUser.getUserId());
//
//        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
//        SysDept dept =sysUser.getDept();
//        if(dept.getDeptName().indexOf("市场")>=0){
//            returnApply.setUserId(sysUser.getUserId());
//        }
        startPage();
        List<ReturnApplyDto> list = returnApplyService.childList(returnApply);
        return getDataTable(list);
    }



    /**
     * 导出回库申请单列表
     */
    @PreAuthorize("@ss.hasPermi('module:apply:export')")
    @Log(title = "回库申请单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReturnApply returnApply)
    {
        List<ReturnApplyDto> list = returnApplyService.selectReturnApplyList(returnApply);
        ExcelUtil<ReturnApplyDto> util = new ExcelUtil<ReturnApplyDto>(ReturnApplyDto.class);
        util.exportExcel(response, list, "回库申请单数据");
    }

    /**
     * 获取回库申请单详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:apply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(returnApplyService.selectReturnApplyById(id));
    }

    /**
     * 新增回库申请单
     */
    @PreAuthorize("@ss.hasPermi('module:apply:add')")
    @Log(title = "回库申请单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReturnApply returnApply)
    {
        return toAjax(returnApplyService.insertReturnApply(returnApply));
    }

    /**
     * 修改回库申请单
     */
    @PreAuthorize("@ss.hasPermi('module:apply:edit')")
    @Log(title = "回库申请单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReturnApply returnApply)
    {
        return toAjax(returnApplyService.updateReturnApply(returnApply));
    }

    /**
     * 删除回库申请单
     */
    @PreAuthorize("@ss.hasPermi('module:apply:remove')")
    @Log(title = "回库申请单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(returnApplyService.deleteReturnApplyByIds(ids));
    }

    /**
     * 获取申请单编号
     */
    @Log(title = "获取申请单编号", businessType = BusinessType.UPDATE)
//    @PreAuthorize("@ss.hasPermi('module:apply:getNo')")
    @PostMapping("/getNo")
    public AjaxResult getNo(@RequestBody ReturnApply outboundOrder)
    {
        return AjaxResult.success(returnApplyService.getNo(outboundOrder));
    }
}
